package manage;

import logIn.MysqlConnectionTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 员工离职界面功能实现
 * @Author Jian Chen long
 * @Date 2021/6/18 13:15
 */
public class dropWorker extends JFrame {
    private JPanel panel = new JPanel();
    private JLabel logNum = new JLabel("工号");
    private JTextField logNumText = new JTextField(18);

    private JPanel panel1 = new JPanel(new GridLayout(1,2,0,0));
    private JButton yesBtn = new JButton("确定");
    private JButton noBtn = new JButton("取消");

    public dropWorker(){
        panel.add(logNum);
        panel.add(logNumText);
        panel1.add(yesBtn);
        panel1.add(noBtn);
        this.add(panel);
        this.add(panel1,BorderLayout.SOUTH);
        this.setTitle("员工离职");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(450,180);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        /**
         * 确定按钮功能实现
         */
        yesBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 得到员工的工号
                String num = logNumText.getText();
                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                try{
                    conn = MysqlConnectionTool.getConnection();
                    // 判断信息是否填写完整,不完整弹出消息框
                    if (num.length() == 0){
                        JOptionPane.showMessageDialog(null,"填写完整信息");
                    }else {
                        // 向管理员再次确定是否要离职(删除数据)
                        int n = JOptionPane.showConfirmDialog(null,"确定离职嘛？","离职",JOptionPane.YES_NO_OPTION);
                        // 点击确定,离职成功,数据库users表中删除该员工数据
                        if (n == 0){
                            String sql = "DELETE FROM users WHERE userno = " + num;
                            ps = conn.prepareStatement(sql);
                            int flag = ps.executeUpdate();
                            if (flag == 1){
                                JOptionPane.showMessageDialog(null,"离职成功");
                                logNumText.setText(null);
                            }else {
                                JOptionPane.showMessageDialog(null,"没有这个员工");
                                logNumText.setText(null);
                            }
                        }else {
                            // 点击取消,取消删除,返回界面
                            logNumText.setText(null);
                            return;
                        }
                    }
                }catch (Exception E){
                    JOptionPane.showMessageDialog(null,"输入正确的工号");
                    logNumText.setText(null);
                    return;
                } finally {
                    MysqlConnectionTool.close(conn,ps,rs);
                }
            }
        });
        /**
         * 取消按钮功能实现
         */
        noBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"系统退出");
                System.exit(0);
            }
        });
    }
}
